///////////////////////////////////////////////////////////////
// File: "CCustomizeState.cpp"
// Author: Ryan Burkevich (RB)
// Purpose: To contain all player customization functionality.
///////////////////////////////////////////////////////////////

#include "CCustomizeState.h"
#include "../Wrappers/CSGD_DirectInput.h"
#include "../Wrappers/CSGD_TextureManager.h"
#include "../Wrappers/CSGD_Direct3D.h"
#include "CWiimote.h"
#include "CProfileManager.h"
#include "CBitmapFontEngine.h"
#include "CProfileSelectState.h"
#include "CGame.h"
#include "CAnimationManager.h"
#include "CSoundManager.h"

CCustomizeState::CCustomizeState()
{
	m_nUDChoice = 0;
	m_nFontID = -1;
	m_nWhereIsCursor = START;
	m_nBlobID = -1;
	m_nSelectedItem = NONE;
	m_bItsAddingTime = false;
	m_bCanColorChange = false;

	for(int i = 0; i < 3; i++)
		m_vItemIDs.push_back(-1);
}

CCustomizeState::~CCustomizeState()
{

}

void CCustomizeState::Enter()
{
	CAnimationManager* pAM = CAnimationManager::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();
	m_nUDChoice = 0;

	m_cScreen.LoadScreen("Screens/BS_LevelSelectScreen.xml");
	m_nFontID = CBitmapFontEngine::GetInstance()->LoadFont("BS_TechDemoTitle.xml");
	pAM->AnimationPlayer("bIdle",true);
	

	m_vItemIDs[RHINO_HORN] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/PlayerImages/BS_Horn.png");
	m_vItemIDs[WHITE_HORN] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/PlayerImages/BS_WhiteHorn.png");
	m_vItemIDs[BOW] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/PlayerImages/BS_Bow.png");

	m_nWhereIsCursor = START;
	m_bItsAddingTime = false;
	m_bCanColorChange = false;
	m_bTempItem = false;
}

void CCustomizeState::Exit()
{
	m_nUDChoice = 0;

	for(int i = 0; i < 3; i++)
		CSGD_TextureManager::GetInstance()->ReleaseTexture(m_vItemIDs[i]);
	m_nWhereIsCursor = START;
	m_bItsAddingTime = false;
	m_bCanColorChange = false;
	m_bTempItem = false;
}

bool CCustomizeState::Input(float fElapsedTime)
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();
	CWiimote* pWii = CWiimote::GetInstance();

	if (pWii->IsButtonJustPressed(WII_HOME) || pDI->KeyPressed(DIK_ESCAPE))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
		if(m_nWhereIsCursor != START)
		{
			m_nWhereIsCursor = START;
			m_bTempItem = false;
		}
		else
		{
			CProfileSelectState::GetInstance()->SetFromCustomize(true);
			CGame::GetInstance()->ChangeState(CProfileSelectState::GetInstance());
		}
	}

	if(m_nWhereIsCursor == START)
	{
		if(m_nSelectedItem != NONE)
			m_bTempItem = true;

		if(pDI->KeyPressed(DIK_RETURN) || pWii->IsButtonJustPressed(WII_A))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
			switch(m_nUDChoice)
			{
			case 0:
				if(m_nSelectedItem == NONE)
				{
					m_nUDChoice = 0;
					m_nWhereIsCursor = ADD;
					m_bTempItem = true;
				}
				break;
			case 1:
				if(m_nSelectedItem != NONE)
				{
					m_nWhereIsCursor = REMOVE;
					m_nUDChoice = 0;
				}
				break;			
			case 2:
				m_nUDChoice = 0;
				m_nWhereIsCursor = COLOR;
				break;
			case 3:
				CProfileSelectState::GetInstance()->SetFromCustomize(true);
				CGame::GetInstance()->ChangeState(CProfileSelectState::GetInstance());
				break;
			}
		}

		if(pDI->KeyPressed(DIK_UP) || pWii->IsButtonJustPressed(WII_UP))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nUDChoice--;
		}
		else if(pDI->KeyPressed(DIK_DOWN) || pWii->IsButtonJustPressed(WII_DOWN))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nUDChoice++;
		}

		if(m_nUDChoice > 3) 
			m_nUDChoice = 0;
		else if(m_nUDChoice < 0) 
			m_nUDChoice = 3;
	}
	else if(m_nWhereIsCursor == ADD)
	{	
		if(!m_bItsAddingTime)
		{
			if(pDI->KeyPressed(DIK_RETURN) || pWii->IsButtonJustPressed(WII_A))
			{
				CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
				switch(m_nLRChoice)
				{
				case RHINO_HORN:
					m_nSelectedItem = RHINO_HORN;
					m_bItsAddingTime = true;
					break;
				case WHITE_HORN:
					m_nSelectedItem = WHITE_HORN;
					m_bItsAddingTime = true;
					break;
				case BOW:
					m_nSelectedItem = BOW;
					m_bItsAddingTime = true;
					break;				
				}
			}

			if(pDI->KeyPressed(DIK_LEFT) || pWii->IsButtonJustPressed(WII_LEFT))
			{
				CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
				m_nLRChoice--;
			}
			else if(pDI->KeyPressed(DIK_RIGHT) || pWii->IsButtonJustPressed(WII_RIGHT))
			{
				CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
				m_nLRChoice++;
			}

			if(m_nLRChoice > 2) 
				m_nLRChoice = 0;
			else if(m_nLRChoice < 0) 
				m_nLRChoice = 2;

			pPM->GetCurrentProfile()->SetCurrentItem(m_nLRChoice);

		}
		else if(m_bItsAddingTime)
		{
			if(pDI->KeyPressed(DIK_RETURN) || pWii->IsButtonJustPressed(WII_A))
			{
				CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
				if(m_nUDChoice == 0)
				{
					m_bItsAddingTime = false;
					m_nWhereIsCursor = START;
					m_nSelectedItem = m_nLRChoice;
					pPM->GetCurrentProfile()->SetItem(true);
					pPM->GetCurrentProfile()->SetRotateItem(true);
					pPM->GetCurrentProfile()->SetCurrentItem(m_nSelectedItem);
					m_nLRChoice = 0;
				}
				else if(m_nUDChoice == 1)
				{
					m_bItsAddingTime = false;
					m_nSelectedItem = NONE;
					m_nUDChoice = 0;
				}
			}

			if(pDI->KeyPressed(DIK_UP) || pWii->IsButtonJustPressed(WII_UP))
			{
				CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
				m_nUDChoice--;
			}
			else if(pDI->KeyPressed(DIK_DOWN) || pWii->IsButtonJustPressed(WII_DOWN))
			{
				CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
				m_nUDChoice++;
			}

			if(m_nUDChoice > 1) 
				m_nUDChoice = 0;
			else if(m_nUDChoice < 0) 
				m_nUDChoice = 1;
		}
	}
	else if(m_nWhereIsCursor == REMOVE)
	{
		if(pDI->KeyPressed(DIK_RETURN) || pWii->IsButtonJustPressed(WII_A))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
			if(m_nUDChoice == 0)
			{
				m_nSelectedItem = NONE;
				pPM->GetCurrentProfile()->SetItem(false);
				pPM->GetCurrentProfile()->SetRotateItem(false);
				m_nWhereIsCursor = START;
				m_bTempItem = false;
			}
			else if(m_nUDChoice == 1)
			{
				m_nWhereIsCursor = START;
			}
		}	

		if(pDI->KeyPressed(DIK_UP) || pWii->IsButtonJustPressed(WII_UP))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nUDChoice--;
		}
		else if(pDI->KeyPressed(DIK_DOWN) || pWii->IsButtonJustPressed(WII_DOWN))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nUDChoice++;
		}

		if(m_nUDChoice > 1) 
			m_nUDChoice = 0;
		else if(m_nUDChoice < 0) 
			m_nUDChoice = 1;
	}
	else if(m_nWhereIsCursor == COLOR)
	{
		m_bTempItem = false;

		if(pDI->KeyPressed(DIK_RETURN) || pWii->IsButtonJustPressed(WII_A))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
			if(m_nUDChoice == 0)
			{
				pPM->GetCurrentProfile()->SetColor(0);
				m_nUDChoice = 0;
				m_nWhereIsCursor = START;
			}
			else if(m_nUDChoice == 1)
			{
				pPM->GetCurrentProfile()->SetColor(1);
				m_nUDChoice = 0;
				m_nWhereIsCursor = START;
			}
			else if(m_nUDChoice == 2)
			{
				pPM->GetCurrentProfile()->SetColor(2);
				m_nUDChoice = 0;
				m_nWhereIsCursor = START;
			}
		}

		if(pDI->KeyPressed(DIK_UP) || pWii->IsButtonJustPressed(WII_UP))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nUDChoice--;
		}
		else if(pDI->KeyPressed(DIK_DOWN) || pWii->IsButtonJustPressed(WII_DOWN))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nUDChoice++;
		}

		if(m_nUDChoice > 2) 
			m_nUDChoice = 0;
		else if(m_nUDChoice < 0) 
			m_nUDChoice = 2;
	}

	return true;
}

void CCustomizeState::Update(float fElapsedTime)
{
	CAnimationManager::GetInstance()->UpdateAnimation("bIdle",fElapsedTime);
}

void CCustomizeState::Render()
{
	m_cScreen.RenderScreen();

	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();

	int TopX= m_cScreen.GetPosObjMembers("Top",XPOS);
	int TopY= m_cScreen.GetPosObjMembers("Top",YPOS);

	int LeftX = m_cScreen.GetPosObjMembers("Left",XPOS);
	int LeftY = m_cScreen.GetPosObjMembers("Left",YPOS);

	int RightX = m_cScreen.GetPosObjMembers("Right",XPOS);
	int RightY = m_cScreen.GetPosObjMembers("Right",YPOS);

	pBFE->RenderString("Customize", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), TopX, TopY+25, 0);

	if(m_nWhereIsCursor == START)
	{
		CAnimationManager::GetInstance()->DrawAnimation("bIdle", RightX+115, RightY+100, 1, 1, 0, 0, 0, pPM->GetCurrentProfile()->GetDWORDColor());

		switch(m_nUDChoice)
		{
		case 0:
			pBFE->RenderString("Add", m_nFontID, 0, 0xffffff00, LeftX, LeftY+45, 0);
			pBFE->RenderString("Remove", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+80, 0);
			pBFE->RenderString("Color", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+115, 0);
			pBFE->RenderString("Done", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+150, 0);
			break;
		case 1:
			pBFE->RenderString("Add", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+45, 0);
			pBFE->RenderString("Remove", m_nFontID, 0, 0xffffff00, LeftX, LeftY+80, 0);
			pBFE->RenderString("Color", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+115, 0);
			pBFE->RenderString("Done", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+150, 0);
			break;
		case 2:
			pBFE->RenderString("Add", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+45, 0);
			pBFE->RenderString("Remove", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+80, 0);
			pBFE->RenderString("Color", m_nFontID, 0, 0xffffff00, LeftX, LeftY+115, 0);
			pBFE->RenderString("Done", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+150, 0);
			break;
		case 3:
			pBFE->RenderString("Add", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+45, 0);
			pBFE->RenderString("Remove", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+80, 0);
			pBFE->RenderString("Color", m_nFontID, 0, 0xff00ff00, LeftX, LeftY+115, 0);
			pBFE->RenderString("Done", m_nFontID, 0, 0xffffff00, LeftX, LeftY+150, 0);
			break;
		}
	}
	else if(m_nWhereIsCursor == ADD)
	{
		CAnimationManager::GetInstance()->DrawAnimation("bIdle", LeftX+115, LeftY+100, 1, 1, 0, 0, 0, pPM->GetCurrentProfile()->GetDWORDColor());

		if(!m_bItsAddingTime)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[RHINO_HORN], RightX+55, RightY+55, -1, 1, 0, 0, 0, 0, 0xffffffaa);
			CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[WHITE_HORN], RightX+90, RightY+45, 1, 1, 0, 0, 0, 0, 0xffffffaa);
			CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[BOW], RightX+225, RightY+55, -1.5f, 1.5f, 0, 0, 0, 0, 0xffffffaa);

			switch(m_nLRChoice)
			{
			case RHINO_HORN:
				CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[RHINO_HORN], RightX+55, RightY+55, -1, 1, 0, 0, 0, 0, 0xffffffff);
				pBFE->RenderString("Nose Horn", m_nFontID, 0, 0xff00ff00, RightX+125, RightY+150, 0, .8f);
				break;
			case WHITE_HORN:
				CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[WHITE_HORN], RightX+90, RightY+45, 1, 1, 0, 0, 0, 0, 0xffffffff);
				pBFE->RenderString("Back Horn", m_nFontID, 0, 0xff00ff00, RightX+125, RightY+150, 0, .8f);
				break;
			case BOW:
				CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[BOW], RightX+225, RightY+55, -1.5f, 1.5f, 0, 0, 0, 0, 0xffffffff);
				pBFE->RenderString("Bow", m_nFontID, 0, 0xff00ff00, RightX+225, RightY+150, 0, .8f);
				break;				
			}
		}
		else if(m_bItsAddingTime)
		{
			if(m_nSelectedItem == BOW)
				CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[m_nSelectedItem], RightX+130, RightY+80, -1.5f, 1.5f, 0, 0, 0, 0, 0xffffffff);
			else if(m_nSelectedItem == RHINO_HORN)
				CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[m_nSelectedItem], RightX+130, RightY+80, -1, 1, 0, 0, 0, 0, 0xffffffff);
			else if(m_nSelectedItem == WHITE_HORN)
				CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[m_nSelectedItem], RightX+80, RightY+65, 1, 1, 0, 0, 0, 0, 0xffffffff);

			pBFE->RenderString("Add?", m_nFontID, 0, 0xff00ff00, RightX+60, RightY, 0);
			if(m_nUDChoice == 0)
			{
				pBFE->RenderString("Yes", m_nFontID, 0, 0xffffff00, RightX+70, RightY+150, 0);
				pBFE->RenderString("No", m_nFontID, 0, 0xff00ff00, RightX+80, RightY+200, 0);
			}
			else if(m_nUDChoice == 1)
			{
				pBFE->RenderString("Yes", m_nFontID, 0, 0xff00ff00, RightX+70, RightY+150, 0);
				pBFE->RenderString("No", m_nFontID, 0, 0xffffff00, RightX+80, RightY+200, 0);
			}
		}

	}
	else if(m_nWhereIsCursor == REMOVE)
	{
		if(m_nSelectedItem == BOW)
			CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[m_nSelectedItem], LeftX+130, LeftY+80, -1.5f, 1.5f, 0, 0, 0, 0, 0xffffffff);
		else if(m_nSelectedItem == RHINO_HORN)
			CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[m_nSelectedItem], LeftX+130, LeftY+80, -1, 1, 0, 0, 0, 0, 0xffffffff);
		else if(m_nSelectedItem == WHITE_HORN)
			CSGD_TextureManager::GetInstance()->Draw(m_vItemIDs[m_nSelectedItem], LeftX+80, LeftY+65, 1, 1, 0, 0, 0, 0, 0xffffffff);

		CAnimationManager::GetInstance()->DrawAnimation("bIdle", RightX+115, RightY+100, 1, 1, 0, 0, 0, pPM->GetCurrentProfile()->GetDWORDColor());

		pBFE->RenderString("Remove?", m_nFontID, 0, 0xff00ff00, LeftX+30, LeftY, 0);
		if(m_nUDChoice == 0)
		{
			pBFE->RenderString("Yes", m_nFontID, 0, 0xffffff00, LeftX+70, LeftY+150, 0);
			pBFE->RenderString("No", m_nFontID, 0, 0xff00ff00, LeftX+80, LeftY+200, 0);
		}
		else if(m_nUDChoice == 1)
		{
			pBFE->RenderString("Yes", m_nFontID, 0, 0xff00ff00, LeftX+70, LeftY+150, 0);
			pBFE->RenderString("No", m_nFontID, 0, 0xffffff00, LeftX+80, LeftY+200, 0);
		}
	}
	else if(m_nWhereIsCursor == COLOR)
	{
		CAnimationManager::GetInstance()->DrawAnimation("bIdle", LeftX+115, LeftY+100, 1, 1, 0, 0, 0, pPM->GetCurrentProfile()->GetDWORDColor());

		pBFE->RenderString("Green", m_nFontID, 0, 0xff00ff00, RightX+35, RightY+35, 0);
		pBFE->RenderString("Red", m_nFontID, 0, 0xffff0000, RightX+35, RightY+70, 0);
		pBFE->RenderString("Blue", m_nFontID, 0, 0xff0000ff, RightX+35, RightY+105, 0);

		if(m_nUDChoice == 0)//Green
			CAnimationManager::GetInstance()->DrawAnimation("bIdle", RightX+10, RightY+35, 1, 1, 0, 0, 0, 0xff00ff00);
		else if(m_nUDChoice == 1)//Red
			CAnimationManager::GetInstance()->DrawAnimation("bIdle", RightX+10, RightY+70, 1, 1, 0, 0, 0, 0xffff0000);
		else if(m_nUDChoice == 2)//Blue
			CAnimationManager::GetInstance()->DrawAnimation("bIdle", RightX+10, RightY+105, 1, 1, 0, 0, 0, 0xff0000ff);
	}
}
